/*
 * MP3Handler.java
 *
 * Created on 8 September 2006, 2:35
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package fasea.index.filehandler;

import java.io.File;
import java.io.IOException;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.farng.mp3.MP3File;
import org.farng.mp3.TagException;
import org.farng.mp3.id3.ID3v1;

/**
 *
 * @author gvasil
 */
class MP3Handler extends FileHandler {

    public Document getTypedDocument(File file) throws FileHandlerException {
        MP3File mp3;

        try {
            mp3 = new MP3File(file);
        } catch (TagException ex) {
            throw new FileHandlerException(
                    "Tag error: "
                    + file.getAbsolutePath(), ex);
        } catch (IOException ex) {
            throw new FileHandlerException(
                    "IO error: "
                    + file.getAbsolutePath(), ex);
        } catch (ArrayIndexOutOfBoundsException ex) {
            mp3 = null;
        } catch (StringIndexOutOfBoundsException ex) {
            mp3 = null;
        }

        org.apache.lucene.document.Document doc = createDefaultLuceneDocument(file);

        try {
            String path = file.getCanonicalPath();
            String filename = path.substring(path.lastIndexOf(File.separatorChar) + 1);

            doc.add(new Field("content", filename, Field.Store.NO,
                    Field.Index.ANALYZED, Field.TermVector.YES));

        } catch (IOException e) {
            throw new FileHandlerException(e.getMessage());
        }

        String fileName = file.getName();
        int dotIndex = fileName.lastIndexOf(".");


        ID3v1 id3v1 = null;
        if (mp3 != null) {
            id3v1 = mp3.getID3v1Tag();
        }

        if (id3v1 != null) {
            String artist = id3v1.getArtist();
            doc.add(new Field("content", artist, Field.Store.NO,
                    Field.Index.ANALYZED, Field.TermVector.YES));
            doc.add(new Field("artist", artist, Field.Store.YES, Field.Index.NOT_ANALYZED));

            String title = id3v1.getTitle();
            doc.add(new Field("content", title, Field.Store.NO,
                    Field.Index.ANALYZED, Field.TermVector.YES));
            doc.add(new Field("title", title, Field.Store.YES, Field.Index.NOT_ANALYZED));

            String album = id3v1.getAlbum();
            doc.add(new Field("content", album, Field.Store.NO,
                    Field.Index.ANALYZED, Field.TermVector.YES));
            doc.add(new Field("album", album, Field.Store.YES, Field.Index.NOT_ANALYZED));

//          Byte genre = id3v1.getGenre();
//          doc.add(new Field("content", genre, Field.Store.NO, Field.Index.TOKENIZED));
//          doc.add(new Field("genre", genre, Field.Store.YES, Field.Index.UN_TOKENIZED));

            String year = id3v1.getYear();
            doc.add(new Field("content", year, Field.Store.NO,
                    Field.Index.ANALYZED, Field.TermVector.YES));
            doc.add(new Field("year", year, Field.Store.YES, Field.Index.NOT_ANALYZED));
        }
        return doc;
    }
}
